package com.heima.algorithm.recursion;

/**
 * 指定位置从尾逆向打印字符
 */
public class ReversePrintString {
    public void reversePrintString(String string) {
        reversePrintString(0, string.length() - 1, string, true);
    }

    public void reversePrintString(int startIndex, String string) {
        reversePrintString(startIndex, string.length() - 1, string, true);
    }

    public void reversePrintString(int startIndex, int endIndex, String string) {
        reversePrintString(startIndex, endIndex, string, true);
    }

    private void reversePrintString(int startIndex, int endIndex, String string, boolean isLast) {
        if (startIndex == endIndex + 1) {
            return;
        }
        reversePrintString(startIndex + 1, endIndex, string, false);
        if (isLast) {
            System.out.println(string.charAt(startIndex));
            return;
        }
        System.out.print(string.charAt(startIndex));
    }

    public static void main(String[] args) {
        ReversePrintString r = new ReversePrintString();
        r.reversePrintString(0, "hello");
        r.reversePrintString(2, "hello");
        r.reversePrintString("hello");
        r.reversePrintString(1, 2, "hello");
    }
}
